package org.lanqiao.sjx.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.lanqiao.bean.Work;
import org.lanqiao.sjx.dao.WorkDao;
import org.lanqiao.sjx.dao.impl.WorkImpl;
import org.lanqiao.tools.BaseDao;
import org.lanqiao.tools.BaseServlet;

import com.google.gson.Gson;
import com.sun.org.apache.bcel.internal.generic.NEW;

@WebServlet("/Workservlet")
public class Workservlet extends BaseServlet {
	Gson gson=new Gson();
	WorkDao dao=new WorkImpl();
	private Integer deleteWork;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		Object id = request.getSession().getAttribute("id");
		Object typeU = request.getSession().getAttribute("typeU");
		this.request = request;
		this.response = response;

		String type = getString("type");
		redata.setMsg("");
		if (!isNull("type")) {
			switch (type) {
			case "insert":
				insertWork();
				break;
			case "delete":
				deleteWork();
				break;
			case "update":
				updateWork();
				break;
			case "select":
				selectWork();
				break;
			case "selectnewwork":
				selectnewWork();
				break;

			default:
				break;
			}
		}
			
		else {
				redata.setCode(3);
				redata.setData(null);
			}
			System.out.println(redata);
			response.getWriter().write(new Gson().toJson(redata));		

	}

	private void selectnewWork() {
		String sql="select distinct w.* from work as w,resume as r where w.id=r.jobIds";
		
		try {
			Map<String, Object> redata= BaseDao.query.query(sql, new MapHandler());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}

	private void selectWork() {
		System.out.println("select查询");
		Integer userId=getInt("userId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData("");
			return;
		}
		
		List<Work> selectWork = dao.selectWork(userId);
		redata.setCode(2);
		redata.setData(selectWork);
		redata.setMsg("查询成功");
	}
	

	private void updateWork() {
		System.out.println("updatetwork");
		Integer wid = getInt("id");
		String compName = getString("compName");
		String jobName = getString("jobName");
		Integer startYear = getInt("startYear");
		Integer startMonth = getInt("startMonth");
		Integer endYear = getInt("endYear");
		Integer endMonth = getInt("endMonth");
		System.out.println(compName);
		System.out.println(wid);
		if (redata.getMsg() != "") {
			redata.setCode(3);
			redata.setData(null);
			return;
		}
		Integer i=dao.updateWork(new Work(wid, compName, jobName, startYear, startMonth, endYear, endMonth));
		System.out.println(i);
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("修改成功");
		}
	}
	

	private void deleteWork() {
		Integer id=getInt("id");
		Integer userId = getInt("userId");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			return;
		}
		
		Integer i=dao.deleteWork(id, userId);
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("删除成功");	
		}
		
	}

	private void insertWork() {
		System.out.println("insertwork");
		Integer userId = getInt("userId");
		String compName = getString("compName");
		String jobName = getString("jobName");
		Integer startYear = getInt("startYear");
		Integer startMonth = getInt("startMonth");
		Integer endYear = getInt("endYear");
		Integer endMonth = getInt("endMonth");
		if (redata.getMsg() != "") {
			redata.setCode(3);
			return;
		}
		Integer i=dao.insertWork(new Work(userId, compName, jobName, startYear, startMonth, endYear, endMonth));
		if (i>0) {
			redata.setCode(2);
			redata.setData(i);
			redata.setMsg("添加成功");
		}
	}

}
